package com.example.lib.extend

import java.math.BigDecimal

fun String?.firstZeroAutoInt(default: Int = 0): Int {
    return if (isNullOrEmpty()) default
    else {
        if (startsWith("0")) substring(1).autoInt(default)
        else autoInt(default)
    }
}

fun String?.autoInt(default: Int = 0): Int {
    return if (isNullOrEmpty()) default
    else toIntOrNull() ?: default
}

fun String?.autoLong(default: Long = 0L): Long {
    return if (isNullOrEmpty()) default
    else toLongOrNull() ?: default
}

fun String?.autoDouble(default: Double = 0.0): Double {
    return if (isNullOrEmpty()) default
    else toDoubleOrNull() ?: default
}

fun String?.autoBigDecimal(default: BigDecimal = BigDecimal.ZERO): BigDecimal {
    return if (isNullOrEmpty()) default
    else toBigDecimalOrNull() ?: default
}

fun Int?.isTrue() = this == 1

fun Int?.remindMaxNumber() = if ((this ?: 0) > 99) "99+" else this.toString()

fun BigDecimal.getString() = stripTrailingZeros().toPlainString() ?: ""

fun Int.toFormatTime(): String {
    return if (this < 10) "0${this}"
    else this.toString()
}